/**
 *
 */
package pt.iscte.iul.dcti.pa.gspas.util;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author Gedsimon Pereira - 58554
 * @contact gspas@iscte.pt
 */
public class SwingUtils {

	/**
	 * 
	 * @param name
	 * @param state
	 * @return A boolean component
	 */
	public static JCheckBox createBoolean(String name, boolean state) {
		JCheckBox tmpCheckBox = new javax.swing.JCheckBox();
		tmpCheckBox.setText(name);
		tmpCheckBox.setSelected(state);
		return tmpCheckBox;
	}

	/**
	 * 
	 * @param name
	 * @return A label component
	 */
	public static JLabel createLabel(String name) {
		JPanel tempPanel = new javax.swing.JPanel();
		tempPanel.setLayout(new java.awt.GridLayout(1, 1));
		JLabel tmpLabel = new javax.swing.JLabel();
		tmpLabel.setText(name);
		tempPanel.add(tmpLabel);
		return tmpLabel;
	}

	public static JTextField createTxtField(String txt) {
		JTextField tmpField = new javax.swing.JTextField();
		tmpField.setText(txt);
		return tmpField;
	}

	public static JComboBox createCombobox() {
		JComboBox tmpCombo = new JComboBox();
		tmpCombo.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "[Item]  Item One" }));
		return tmpCombo;
	}

	public static JButton createButton(String txt) {
		JButton tmbBtn = new JButton();
		tmbBtn.setText(txt);
		return tmbBtn;
	}

	public static Object getValueFromComponent(Property property,
			JComponent jComponent) {
		try {
			switch (property.getType()) {
			case STRING:
				return ((JTextField) jComponent).getText();
			case BOOLEAN:
				return ((JCheckBox) jComponent).isSelected();
			case INT:
				return Integer.parseInt(((JTextField) jComponent).getText());
			case DOUBLE:
				return Double.parseDouble(((JTextField) jComponent).getText());
			case ENUM:
				return null;
			default:
				break;
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return null;
	}
}
